import 'package:flutter/material.dart';

import 'base_tab_config.dart';

class BaseTabBarPage extends State with SingleTickerProviderStateMixin {
  /// tab
  final List<BaseTabConfig> tabs;

  ///初始页面
  final int initialIndex;

  /// tab 控制
  TabController tabController;

  BaseTabBarPage(this.tabs, {this.initialIndex = 0}) {
    tabController = TabController(length: tabs.length, initialIndex: this.initialIndex, vsync: this);
  }

  List<Widget> get tabWidget => tabs.map((item) => item.child).toList();

  void addListener(VoidCallback listener) {
    tabController.addListener(listener);
  }

  Widget buildTabBar(BuildContext context, {Function(int index) onTap, bool isScrollable, Color color = Colors.white, List<String> titleList}) {
    if (titleList == null || titleList.isEmpty) {
      titleList = this.tabs.map((e) => e.text).toList();
    }
    return Ink(
      padding: EdgeInsets.zero,
      color: color,
      child: TabBar(
        isScrollable: isScrollable ?? false,
        tabs: titleList.map((item) => Text(item)).toList(),
        controller: tabController,
        onTap: (index) {
          if (onTap != null) onTap(index);
        },
      ),
    );
  }

  Widget buildTabView() {
    return TabBarView(controller: tabController, children: tabWidget);
  }

  @override
  void dispose() {
    // this.tabController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return buildTabView();
  }
}
